import {combineReducers, createStore, applyMiddleware, compose} from "redux";
import CityReducer from "./reducers/CityReducer";
import TabbarReducer from "./reducers/TabbarReducer";
import CinemaReducer from "./reducers/CinemaReducer";
import {thunk} from 'redux-thunk';

const reducer = combineReducers({
    CityReducer,
    TabbarReducer,
    CinemaReducer
})

// 简易版createStore实现
// const simpleCreateStore = (reducer) => {
//
//     // 保存回调函数
//     const callbackList = []
//
//     // 获取初始状态
//     let state = reducer(undefined, {})
//
//     // 订阅的回调函数
//     const subscribe = callback => {
//         callbackList.push(callback)
//     }
//
//     const dispatch = action => {
//         // 更新状态
//         state = reducer(state, action)
//
//         // 循环执行订阅的回调方法
//         for (let i = 0; i < callbackList.length; i++) {
//             callbackList[i]()
//         }
//     }
//
//     const getState = () => {
//         return state
//     }
//     return {
//         subscribe,
//         dispatch,
//         getState
//     }
// }

// redux浏览器插件
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
// applyMiddleware 应用中间件thunk  thunk获取异步结果
const store = createStore(reducer, composeEnhancers(applyMiddleware(thunk)));

export default store;